package ps.yj.y1j0.data.database;

import ps.yj.y1j0.data.YJOrderBy;
import ps.yj.y1j0.data.YJWhere;

/**
 * Created with IntelliJ IDEA.
 * User: yanjun
 * Date: 13-1-12
 * Time: 下午11:11
 * To change this template use File | Settings | File Templates.
 */
public class YJSqlManagerImpl implements YJSqlManager {
    private static YJSqlManager sqlManage = null;

    private YJSqlManagerImpl() {
    }

    public synchronized static YJSqlManager getInstance() {
        if (sqlManage == null)
            sqlManage = new YJSqlManagerImpl();
        return sqlManage;
    }

    @Override
    public String createInsert(String tableName, String idProperty, String[] properties) {
        int size = properties.length;
        String sql1 = "INSERT INTO " + tableName + " (";
        String sql2 = ") VALUES(";
        for (int i = 0, j = 0; i < size; i++) {
            if (properties[i].equals(idProperty))
                continue;
            if (j++ > 0) {
                sql1 += ", ";
                sql2 += ", ";
            }
            sql1 += properties[i];
            sql2 += "?";
        }
        sql2 += ")";
        return sql1 + sql2;
    }

    @Override
    public String createUpdate(String tableName, String idProperty, String[] properties) {
        int size = properties.length;
        String sql = "UPDATE " + tableName + " SET ";
        for (int i = 0, j = 0; i < size; i++) {
            if (properties[i].equals(idProperty))
                continue;
            if (j++ > 0)
                sql += ", ";
            sql += properties[i] + " = ?";
        }
        sql += "WHERE " + idProperty + " = ?";
        return sql;
    }

    @Override
    public String createDelete(String tableName, String idProperty, int idCount) {
        String sql = "DELETE FROM " + tableName + " WHERE ";
        for (int i = 0; i < idCount; i++) {
            if (i > 0)
                sql += " OR ";
            sql += idProperty + " = ?";
        }
        return sql;
    }

    @Override
    public String createSelect(String sql, YJWhere[] whereArray, YJOrderBy[] orderByArray, String[] groupByArray) {
        String selectSql = sql;
        if (whereArray != null) {
            String and = " ", or = "";
            int i = 0, j = 0;
            for (YJWhere where : whereArray) {
                if (where.getAnd()) {
                    if (i > 0)
                        and += " AND ";
                    and += where.getKey() + where.getType();
                }
            }
            for (YJWhere where : whereArray) {
                if (!where.getAnd()) {
                    if (j == 0)
                        or += " AND ( ";
                    else
                        or += " OR ";
                    or += where.getKey() + where.getType();
                }
            }
            if (j > 0)
                or += " ) ";
            selectSql += and + or;
        }
        if (orderByArray != null) {
            String order = " ORDER BY ";
            int i = 0;
            for (YJOrderBy orderBy : orderByArray) {
                if (i++ > 0)
                    order += " , ";
                order += orderBy.getName() + orderBy.getType();
            }
            selectSql += order;
        }
        if (groupByArray != null) {
            String group = " GROUP BY ";
            int i = 0;
            for (String groupBy : groupByArray) {
                if (i++ > 0)
                    group += " , ";
                group += groupBy;
            }
            selectSql += group;
        }
        return selectSql;
    }

    @Override
    public String createTable(String tableName, String idProperty, String[] properties) {
        String sql = "";
        return sql;
    }

//    @Override
//    public String alertTable(String tableName, String[] properties) {
//        String sql = "";
//        return sql;
//    }

    @Override
    public String dropTable(String tableName) {
        String sql = "DROP TABLE " + tableName;
        return sql;
    }

    @Override
    public String checkTable(String tableName) {
        String sql = "";
        return sql;
    }
}
